import { useUserStore } from "@/stores/userStore"

/*
 * @Author: 谢陈洁
 * @Date: 2025-08-01 16:05:22
 * @LastEditors: 谢陈洁
 * @LastEditTime: 2025-08-04 10:05:21
 * @Description: file content
 */
export const vPermission = {
  mounted(el: HTMLElement, binding: any) {
    const { value } = binding
    if (value && !checkPermission(value)) {
      el.parentNode?.removeChild(el)
    }
  },
  // 组件更新时重新检查权限
  updated(el: HTMLElement, binding: any) { 
    const { value } = binding
    if(value !== binding.oldValue) {
      if(el.parentNode && !checkPermission(value)) {
        el.parentNode.removeChild(el)
      }
    }
  }
}
function checkPermission(permission: string | string[]): boolean {
  const userStore = useUserStore()
  const permissionButtons = userStore.permissionButtons // 动态菜单
  if(Array.isArray(permission)) {
    return permission.some(item => permissionButtons.includes(item))
  }
  return permissionButtons.includes(permission)
}
